﻿using RimWorld;
using System;
using Verse;
using Verse.AI;


namespace Miao.Stand
{
    /// <summary>
    /// 找到最近温度的对象
    /// </summary>
    public class JobGiver_TrackingHeat : ThinkNode_JobGiver
    {
        public const float fireRanage = 5f;
        public const float heatPusherBuilding = 10f;
        public const float heatPusherPawn = 30f;
        protected override Job TryGiveJob(Pawn pawn)
        {
            //pawn.Map.listerThings.ThingsMatching()

            Thing fire = GenClosest.ClosestThingReachable(pawn.Position, pawn.Map, ThingRequest.ForGroup(ThingRequestGroup.Fire), PathEndMode.Touch, TraverseParms.For(pawn), fireRanage);
            Thing buliding = GenClosest.ClosestThing_Regionwise_ReachablePrioritized(pawn.Position, pawn.Map, ThingRequest.ForGroup(ThingRequestGroup.BuildingArtificial), PathEndMode.Touch, TraverseParms.For(pawn), heatPusherBuilding, ValidatorTemperature, GetBuildingHeat);
            Thing heatPawn = GenClosest.ClosestThing_Regionwise_ReachablePrioritized(pawn.Position, pawn.Map, ThingRequest.ForGroup(ThingRequestGroup.Pawn), PathEndMode.Touch, TraverseParms.For(pawn), heatPusherPawn, ValidatorPawn, GetPawnHeat);
            if (fire != null)
            {
                Job job = JobMaker.MakeJob(StandJobDefOf.TrackingHeat, fire, 1000);
                job.targetB = pawn;
                return job;
            }
            else if (buliding!=null)
            {
                Job job = JobMaker.MakeJob(StandJobDefOf.TrackingHeat, buliding, 1000);
                job.targetB = pawn;
                return job;
            }
            else if(heatPawn!=null)
            {
                Job job = JobMaker.MakeJob(StandJobDefOf.TrackingHeat, heatPawn, 1000);
                job.targetB = pawn;
                return job;
            }
            return null;
            bool ValidatorPawn(Thing t)
            {

                Pawn p = (t as Pawn);


                if (p.Downed)
                {
                    return false;
                }
                else if (p.HostileTo(pawn) || p.Faction == null)//是否敌对或是无派系
                {
                    return true;
                }
                return false;
            }
            bool ValidatorTemperature(Thing t)
            {
                CompHeatPusher compHeatPusher =  t.TryGetComp<CompHeatPusher>();
                if(compHeatPusher != null)
                {
                    return true;
                }
                else
                {
                    return false; 
                }
            }

            //CompProperties_HeatPusher
            //GenClosest.ClosestThingReachable
            //RegionTraverser，区域遍历器

        }//要么找范围内包含CompProperties_HeatPusher的类



        float GetBuildingHeat(Thing thing)
        {
            ThingWithComps thingComps = thing as ThingWithComps;
            if (thingComps != null)
            {
                CompHeatPusher compHeatPusher = thingComps.GetComp<CompHeatPusher>();
                if (compHeatPusher != null)
                {
                    return compHeatPusher.Props.heatPushMaxTemperature;
                }
            }
            return -1;
        }

        /// <summary>
        /// 获得最热单位
        /// </summary>
        /// <param name="thing"></param>
        /// <returns></returns>
        float GetPawnHeat(Thing thing)
        {
            if (thing is Pawn)
            {
                Pawn pawn = thing as Pawn;
                ///种族造成升温的简易版
                return pawn.BodySize * (pawn.def.race.Humanlike ? 1f : 0.6f);
            }
            return 0;
        }

        //static Predicate<Thing> HeatBuilding

    }
}
